<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\index\model\Banner;
use app\index\model\Nav;
use think\Controller;
use app\index\model\Nav as NavModel;
use think\Request;
use app\index\model\NavPage as NavPageModel;
use app\index\model\NavList as NavListModel;
use app\index\model\Config as ConfigModel;

class Index extends Controller
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        //导航栏
        $navModel = new NavModel();
        $navList = $navModel->getNavList();
        //页尾
        $basic = ConfigModel::getWebBasic();


        $this->assign('navList', $navList);
        $this->assign('basic', $basic);
    }

    /**
     * 首页
     * @return string
     * @throws \think\Exception
     */
    public function index(NavModel $navModel)
    {
        //新闻，行业新闻
        $news = NavModel::getByName('行业新闻');
        $navId = 0;
        $newId = 0;
        if ($news) {
            $navId = $news->id;
            $newId = $news->pid;
        }
        $newLists = NavListModel::getByNavId($navId);
        //轮播图
        $banners = Banner::getBanners();

        $this->assign('newLists', $newLists);
        $this->assign('newId', $newId);
        $this->assign('banners', $banners);
        return $this->view->fetch();
    }

    /**
     * 单页面
     */
    public function page($id = 0)
    {
        $page = NavPageModel::getPage($id);
        $this->errorPage($page);
        $this->assign('page', $page);
        return $this->fetch();
    }

    /**
     * 列表
     */
    public function lists($id = 0)
    {
        $nav = NavModel::getByPid($id);
        $this->errorPage($nav);
        $this->assign('nav', $nav);
        return $this->fetch();
    }

    /**
     * 列表数据
     */
    public function listAjax($id = 0, Request $request)
    {
        $size = $request->get('size');
        $lists = NavListModel::getLists($id, $size);
        return json($lists);
    }

    /**
     * 列表详情
     */
    public function listShow($id = 0)
    {
        $page = NavListModel::find($id);
        $this->errorPage($page);
        $this->assign('page', $page);
        return $this->fetch();
    }

    /**
     * 关于我们 -- 公司简介
     * @return mixed
     */
    public function about()
    {
        $intro = ConfigModel::getIntro();
        $qualification = ConfigModel::getQualification();
        $this->assign('intro', $intro);
        $this->assign('qualification', $qualification);
        return $this->fetch();
    }

    /**
     * 关于我们 -- 人才加盟
     * @return mixed
     */
    public function join()
    {
        return $this->fetch();
    }

    private function errorPage($page)
    {
        if (!$page) {
            $this->error('页面丢失', null, '', 1);
        }
    }
}
